package club.xyes.zkh.ecommerce.service.impl;

import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.entity.ShopMembershipLevel;
import club.xyes.zkh.ecommerce.dao.mapper.ShopMembershipLevelMapper;
import club.xyes.zkh.ecommerce.service.ShopMembershipLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * Create by 郭文梁 2019/4/18 0018 14:43
 * ShopMembershipLevelServiceImpl
 * 商铺会员等级定义相关业务行为实现
 *
 * @author 郭文梁
 * @data 2019/4/18 0018
 */
@Service
public class ShopMembershipLevelServiceImpl extends BaseServiceImpl<ShopMembershipLevel> implements ShopMembershipLevelService {
    private final ShopMembershipLevelMapper mapper;

    @Autowired
    public ShopMembershipLevelServiceImpl(ShopMembershipLevelMapper mapper) {
        super(mapper);
        this.mapper = mapper;
    }

    @Override
    public List<ShopMembershipLevel> findByShop(Shop shop) {
        return mapper.selectByShopId(shop.getId());
    }

    @Override
    public ShopMembershipLevel highestLevelOfMatching(Shop shop, int fee, int times) {
        List<ShopMembershipLevel> levels = mapper.highestLevelOfMatching(shop.getId(), fee, times);
        Optional<ShopMembershipLevel> level = levels.stream().filter(v -> levelAvailable(v, fee, times)).sorted(Comparator.comparingInt(ShopMembershipLevel::getLevelValue)).reduce((v1, v2) -> v1.getLevelValue() > v2.getLevelValue() ? v1 : v2);
        return level.orElse(null);
    }

    @Override
    public List<ShopMembershipLevel> findByMembershipId(Integer membershipId) {
        ShopMembershipLevel query = new ShopMembershipLevel();
        query.setShopMembershipId(membershipId);
        return findByQuery(query);
    }

    @Override
    public ShopMembershipLevel findByMembershipIdAndLevel(Integer membershipId, Integer levelValue) {
        ShopMembershipLevel query = new ShopMembershipLevel();
        query.setShopMembershipId(membershipId);
        query.setLevelValue(levelValue);
        return findOneByQuery(query);
    }

    /**
     * 检查是否符合会员等级
     *
     * @param level 等级
     * @param fee   消费金额
     * @param times 消费次数
     * @return 是否符合
     */
    private boolean levelAvailable(ShopMembershipLevel level, int fee, int times) {
        if (level == null) {
            return false;
        }
        if (level.getState() != ShopMembershipLevel.STATE_ENABLE) {
            return false;
        }
        int conditionTimes = level.getConditionTimes() == null ? Integer.MIN_VALUE : level.getConditionTimes();
        int conditionAmount = level.getConditionAmount() == null ? Integer.MIN_VALUE : Integer.parseInt(level.getConditionAmount());
        switch (level.getConditionMode()) {
            case ShopMembershipLevel.CONDITION_MODE_BOTH:
                return fee >= conditionAmount && times >= conditionTimes;
            case ShopMembershipLevel.CONDITION_MODE_OR:
                return fee >= conditionAmount || times >= conditionTimes;
            case ShopMembershipLevel.CONDITION_MODE_ONLY_FEE:
                return fee >= conditionAmount;
            case ShopMembershipLevel.CONDITION_MODE_ONLY_TIMES:
                return times >= conditionTimes;
            default:
                throw new IllegalStateException("Unexpected condition mode:" + level.getConditionMode());
        }
    }
}
